package edu.gatech.teamrocket;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.j256.ormlite.dao.Dao;

import edu.gatech.teamrocket.models.Auth;
import edu.gatech.teamrocket.models.Category;
import edu.gatech.teamrocket.models.Task;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ViewTaskActivity extends Activity implements OnClickListener {
	DewApplication app;
	Auth auth;
	
	private Integer taskID;
	private Dao<Task, Integer> taskDao;
	private Task task;
	private MapView mapView;
	private TextView taskName;
	private TextView taskDetails;
	private TextView taskDateTime;
	private TextView taskCategory;
	private TextView taskLocation;
	private Button editButton;
	private Button deleteButton;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		app = (DewApplication) getApplicationContext();
		auth = app.getAuth();
		
		setContentView(R.layout.viewtask);
		
		Bundle extras = getIntent().getExtras();
		taskID = extras.getInt("TASK_ID");
		
		try {
			taskDao = app.getHelper().getTaskDao();
			task = taskDao.queryForId(taskID);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
		taskName = (TextView) findViewById(R.id.textName);
		taskDetails = (TextView) findViewById(R.id.textDetails);
		taskDateTime = (TextView) findViewById(R.id.textDateTime);
		taskCategory = (TextView) findViewById(R.id.textCategory);
		taskLocation = (TextView) findViewById(R.id.textLocation);
		editButton = (Button) findViewById(R.id.editButton);
		deleteButton = (Button) findViewById(R.id.deleteButton);
		String emptyField = "None.";
		
		editButton.setOnClickListener(this);
		deleteButton.setOnClickListener(this);
		
		taskName.setText(task.getName());
		taskDetails.setText((task.getDetails() != null) ? task.getDetails() : emptyField);
		if (task.getTime() != null) {
			DateFormat df;
			if (task.getTime().getHours() == 0 && task.getTime().getMinutes() == 0) { 
				df = new SimpleDateFormat("MMMM dd, yyyy");
			} else {
				df = new SimpleDateFormat("MMMM dd, yyyy, h:mmaa");
			}
			String formattedDate = df.format(task.getTime());
			taskDateTime.setText(formattedDate + "\n" + "Occurs: " + (task.getRecurring() ? "Weekly" : "Once"));
		} else {
			taskDateTime.setText(emptyField);
		}
		taskCategory.setText(task.getCategory().toString());
		taskLocation.setText((task.getLocation() != null) ? task.getLocation() : emptyField);
	}

	@Override
	public void onClick(View item) {
		Intent i;
		switch (item.getId()) {
		case R.id.editButton: // edit button
        	i = new Intent(getApplicationContext(), EditTaskActivity.class);
        	i.putExtra("TASK_ID", task.getId());
			startActivity(i);
			break;
		case R.id.deleteButton: // delete button
			try {
				task.delete();
			} catch (SQLException e) {
				throw new RuntimeException();
			}
			i = new Intent(getApplicationContext(), ListActivity.class);
			Toast.makeText(this, "Deleted!", Toast.LENGTH_SHORT).show();
			startActivity(i);
			break;
		}
	}
}
